package com.jkoss.wine.merchant.controller;

import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.mapper.Condition;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.jkoss.base.controller.BaseController;
import com.jkoss.common.util.CommonUtil;
import com.jkoss.common.util.Constant;
import com.jkoss.common.vo.DwzPageBean;
import com.jkoss.wine.merchant.entity.MerchantProductType;
import com.jkoss.wine.merchant.service.IMerchantProductTypeService;
import com.jkoss.wine.system.entity.MerchantEmployees;
import com.jkoss.wine.system.entity.Role;

import cn.afterturn.easypoi.entity.vo.NormalExcelConstants;
import cn.afterturn.easypoi.excel.entity.ExportParams;
import cn.afterturn.easypoi.excel.entity.enmus.ExcelType;

/**
 * 商品分类 前端控制器
 * 
 * @Author 周华军
 * @Version 1.0, 2018-09-26
 * @See
 * @Since com.jkoss.wine.merchant.controller
 * @Description: TODO
 */
@Controller
@RequestMapping("/merchant/merchantProductType")
public class MerchantProductTypeController extends BaseController {

	@Autowired
	private IMerchantProductTypeService iMerchantProductTypeService;

	/**
	 * 列表
	 * 
	 * @param productTypeName
	 * @param sorting
	 * @param dwzPageBean
	 * @param map
	 * @param request
	 * @param response
	 * @return
	 */
	@RequestMapping("/list")
	@RequiresPermissions("/merchant/merchantProductType/list")
	public String list(String productTypeName, String sorting, DwzPageBean dwzPageBean, ModelMap map, HttpServletRequest request, HttpServletResponse response) {
		String string = request.getParameter("org1.productTypeName");
		String string2 = request.getParameter("org2.sorting");
		// 前端框架bean
		dwzPageBean.getCountResultMap().put("productTypeName", string);
		dwzPageBean.getCountResultMap().put("sorting", string2);
		// 构建排序条件
		Wrapper wrapper = Condition.create().ne("mpt.del", 1).like("mpt.product_type_name", string).like("mpt.sorting", string2);
		// 字段排序
		if (!CommonUtil.isBlank(dwzPageBean.getOrderField()) && !CommonUtil.isBlank(dwzPageBean.getOrderDirection())) {
			dwzPageBean.getCountResultMap().put("orderField", dwzPageBean.getOrderField());
			dwzPageBean.getCountResultMap().put("orderDirection", dwzPageBean.getOrderDirection());
			wrapper.orderBy(dwzPageBean.getOrderField(), CommonUtil.isEquals(dwzPageBean.getOrderDirection(), "asc"));
		} else {
			wrapper.orderBy("mpt.add_time", false);
		}
		Page resultPage = iMerchantProductTypeService.selectVoByPage(dwzPageBean.toPage(), wrapper);
		map.put("dwzPageBean", dwzPageBean.toDwzPageBean(resultPage));
		return "merchant/merchantProductType/list";
	}

	/**
	 * 去新增
	 * 
	 * @param request
	 * @param response
	 * @return
	 */
	@RequestMapping("/toInsert")
	@RequiresPermissions("/merchant/merchantProductType/toInsert")
	public String toInsert(HttpServletRequest request, HttpServletResponse response) {
		return "merchant/merchantProductType/edit";
	}

	@RequestMapping("/insert")
	@RequiresPermissions("/merchant/merchantProductType/toInsert")
	@ResponseBody
	public Object insert(@Valid MerchantProductType merchant_product_type, HttpServletRequest request, HttpServletResponse response) throws Exception {

		// 判断类型名称是否存在
		Wrapper wrapper = Condition.create().eq("product_type_name", merchant_product_type.getProductTypeName());
		MerchantProductType merchantProductType = iMerchantProductTypeService.selectOne(wrapper);
		if (!CommonUtil.isBlank(merchantProductType) && merchantProductType.getDel() == 0) {
			return ajaxError("类型名称已存在");
		}
		// if (!CommonUtil.isBlank(merchantProductType) && merchantProductType.getDel()
		// == 1) {
		// merchantProductType.setDel(0);
		// merchantProductType.setAddTime(CommonUtil.getDate());
		// MerchantEmployees employees = (MerchantEmployees)
		// request.getSession().getAttribute(Constant.SESSION_USER_KEY);
		// merchantProductType.setOperator(employees.getEmployeeId());
		// if (iMerchantProductTypeService.updateById(merchantProductType)) {
		// return ajaxSuccess();
		// } else {
		// return ajaxError();
		// }
		// }
		// 获取当前时间
		merchant_product_type.setAddTime(CommonUtil.getDate());
		// 获取当前操作人
		MerchantEmployees employees = (MerchantEmployees) request.getSession().getAttribute(Constant.SESSION_USER_KEY);
		merchant_product_type.setOperator(employees.getEmployeeName());
		// 是否删除：0-否、1-是
		merchant_product_type.setDel(0);
		if (iMerchantProductTypeService.insertProductType(merchant_product_type)) {
			return ajaxSuccess();
		} else {
			return ajaxError();
		}

	}

	/**
	 * 去修改
	 * 
	 * @param id
	 * @param map
	 * @param request
	 * @param response
	 * @return
	 */
	@RequestMapping("/toUpdate")
	@RequiresPermissions("/merchant/merchantProductType/toUpdate")
	public String toUpdate(String id, ModelMap map, HttpServletRequest request, HttpServletResponse response) {
		map.put("record", iMerchantProductTypeService.selectById(id));
		return "merchant/merchantProductType/edit";
	}

	@RequestMapping("/update")
	@RequiresPermissions("/merchant/merchantProductType/toUpdate")
	@ResponseBody
	public Object update(@Valid MerchantProductType merchant_product_type, HttpServletRequest request, HttpServletResponse response, ModelMap map) throws Exception {
		// 判断类型名称是否存在
		// Wrapper wrapper = Condition.create().eq("product_type_name",
		// merchant_product_type.getProductTypeName());
		// MerchantProductType merchantProductType =
		// iMerchantProductTypeService.selectOne(wrapper);
		// if (!CommonUtil.isBlank(merchantProductType) && merchantProductType.getDel()
		// == 1) {
		// return ajaxError("类型名称已存在");
		// }
		// 判断类型名称是否存在
		// Wrapper wrapper = Condition.create().eq("product_type_name",
		// merchant_product_type.getProductTypeName());
		// MerchantProductType merchantProductType =
		// iMerchantProductTypeService.selectOne(wrapper);
		// if (!CommonUtil.isBlank(merchantProductType)) {
		// return ajaxError("类型名称已存在");
		// }
		// if (!CommonUtil.isBlank(merchantProductType) && merchantProductType.getDel()
		// == 1) {
		// merchantProductType.setDel(0);
		// merchantProductType.setAddTime(CommonUtil.getDate());
		// MerchantEmployees employees = (MerchantEmployees)
		// request.getSession().getAttribute(Constant.SESSION_USER_KEY);
		// merchantProductType.setOperator(employees.getEmployeeId());
		// if (iMerchantProductTypeService.updateById(merchantProductType)) {
		// return ajaxSuccess();
		// } else {
		// return ajaxError();
		// }
		// }
		if (iMerchantProductTypeService.updateProductType(merchant_product_type)) {
			return ajaxSuccess();
		} else {
			return ajaxError();
		}
	}

	/**
	 * 去删除
	 * 
	 * @param id
	 * @param request
	 * @param response
	 * @return
	 * @throws Exception
	 * 
	 */
	@RequestMapping("/delete")
	@RequiresPermissions("/merchant/merchantProductType/delete")
	@ResponseBody
	public Object delete(String[] id, HttpServletRequest request, HttpServletResponse response) throws Exception {

		if (iMerchantProductTypeService.deleteProductType(id)) {
			return ajaxSuccess();
		} else {
			return ajaxError();
		}
	}

	/**
	 * 查询类型名称
	 * 
	 * @param id
	 * @param shop
	 * @param request
	 * @param response
	 * @return
	 */
	@RequestMapping("/getproductTypeName")
	@ResponseBody
	public Object getproductTypeName(String productTypeName, HttpServletRequest request, HttpServletResponse response, ModelMap map) {
		Wrapper wrapper = Condition.create().like("product_type_name", productTypeName).ne("del", 1);
		List<MerchantProductType> list = iMerchantProductTypeService.selectList(wrapper);
		return JSON.toJSONString(list);
	}

	/**
	 * 查询排序名称
	 * 
	 * @param id
	 * @param shop
	 * @param request
	 * @param response
	 * @return
	 */
	@RequestMapping("/getproductTypesorting")
	@ResponseBody
	public Object getproductTypesorting(String sorting, HttpServletRequest request, HttpServletResponse response, ModelMap map) {
		Wrapper wrapper = Condition.create().like("sorting", sorting).ne("del", 1);
		List<MerchantProductType> list = iMerchantProductTypeService.selectList(wrapper);
		return JSON.toJSONString(list);
	}

	/**
	 * 导出商品分类Excel
	 * 
	 * @param map
	 * @param dwzPageBean
	 * @param request
	 * @param response
	 * @return
	 */
	@RequestMapping("/merchantProductTypeexport")
	@RequiresPermissions("/merchant/merchantProductType/merchantProductTypeexport")
	public String merchantProductTypeexport(ModelMap map, DwzPageBean dwzPageBean, HttpServletRequest request, HttpServletResponse response) {
		// 从数据库中查找数据
		Wrapper wrapper = Condition.create().ne("del", 1);
		// 根据用户id获取角色
		boolean isDirector = true;
		List<Role> roles = getRoleList();
		for (Role role : roles) {
			if ("运营主管".equals(role.getName())) {
				isDirector = false;
			}
		}
		if (isDirector) {
			wrapper.eq("maid", getUserId());
		}
		Page page = dwzPageBean.toPage();
		page.setSize(Page.NO_ROW_LIMIT);
		page.setCurrent(1);
		List list = iMerchantProductTypeService.selectPage(page, wrapper).getRecords();
		ExportParams params = new ExportParams(null, "商品分类", ExcelType.XSSF);
		map.put(NormalExcelConstants.DATA_LIST, list); // 数据集合
		map.put(NormalExcelConstants.CLASS, MerchantProductType.class);// 导出实体
		map.put(NormalExcelConstants.PARAMS, params);// 参数
		map.put(NormalExcelConstants.FILE_NAME, "商品分类");// 文件名称
		return NormalExcelConstants.EASYPOI_EXCEL_VIEW;// View名称
	}

}
